<?php
jimport('joomla.application.component.model');
class svModelChucdanh extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllChucdanh()
	{
		$query='SELECT *
			    FROM qlcb_chucdanh
			    WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getChucdanhByID($idchucdanh)
	{
		$query='SELECT *
		FROM qlcb_chucdanh
		WHERE cd_id='.$idchucdanh;
		$result=$this->_loadObject($query);
		return $result;
	}
	//----------------------------them ,sua ,xoa -------------------------------
	function addNewChucdanh($ten,$mieuta)
	{
		$query="INSERT INTO qlcb_chucdanh(cd_ten,cd_mieuta)
		 VALUES('".$ten."','".$mieuta."')";
		$message='Thêm mới ';
		$result=$this->_Execute($query, $message);		
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	// sua
	function editChucdanh($idchucdanh,$ten,$mieuta)
	{
		$query="UPDATE qlcb_chucdanh SET cd_mieuta='".$mieuta."',cd_ten='".$ten."'
	   		    WHERE cd_id=".$idchucdanh;
		$message='Chỉnh sửa ';
		$result=$this->_Execute($query, $message);
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	function delChucdanh($idchucvu)
	{
		$query="DELETE FROM qlcb_chucdanh WHERE cd_id=".$idchucvu;
		$message='Xóa 1 item ';
		$result=$this->_Execute($query, $message);
		return $result;	
	}
	// cac ham phuc vu truy van CSDL
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>